package com.mvp.shq.washer.ui.order;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.OrderAdapter;
import com.mvp.shq.washer.base.BaseFragment;
import com.mvp.shq.washer.entity.OrderInfo;
import com.mvp.shq.washer.rxbus.BusData;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.rxbus.ThreadMode;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public class OrderFragment extends BaseFragment<OrderPresenter, OrderModel> implements OrderConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.title)
    TextView tvTitle;
    @BindView(R.id.division_top)
    View view;
    @BindView(R.id.arl_content)
    AutoRelativeLayout arlContent;
    @BindView(R.id.all_not_login)
    AutoLinearLayout allNotLogin;
    @BindView(R.id.tv_to_login)
    TextView tvLogin;
    @BindView(R.id.arl_not_data)
    AutoRelativeLayout arlNotData;
    private List<OrderInfo> orders;
    private OrderAdapter adapter;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLoadMore;
    private boolean hasMoreData = true;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    public void initView() {
        tvTitle.setText("订单");
        back.setVisibility(View.GONE);
        view.getBackground().setAlpha(100);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.VERTICAL, 30, R.color.white);
        recyclerView.addItemDecoration(divider);
        orders = new ArrayList<>();
        adapter = new OrderAdapter(context,orders);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isLoadMore = false;
                hasMoreData = true;
                pageNo = 1;
                presenter.getOrders(pageNo,pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (!hasMoreData){
                    refreshlayout.finishLoadmore();
                    return;
                }
                isLoadMore = true;
                pageNo ++;
                presenter.getOrders(pageNo,pageSize);
            }
        });
        loginOut();
    }
    @Subscribe(code = EventCode.REFRESH_ORDER)
    public void refreshOrder(){
        refreshLayout.autoRefresh();
    }

    @Subscribe(code = EventCode.UPDATE_USER)
    public void initUser(){
        loginOut();
    }

    @Subscribe(code = EventCode.LOGIN_OUT)
    public void loginOut(){
        if (SpUtil.getUser() == null){
            allNotLogin.setVisibility(View.VISIBLE);
            arlContent.setVisibility(View.GONE);
            arlNotData.setVisibility(View.GONE);
        }else {
            allNotLogin.setVisibility(View.GONE);
            arlContent.setVisibility(View.VISIBLE);
            arlNotData.setVisibility(View.GONE);
            refreshLayout.autoRefresh();
        }
    }

    @Subscribe(code = EventCode.ORDER_ITEM_CLICK, threadMode = ThreadMode.MAIN)
    public void onItemClick(BusData data) {
        String orderNo = orders.get(data.getPosition()).getOrderNo();
        JumpUtil.toOrderDetail(context,orderNo);
    }
    @OnClick(R.id.all_to_login_content)
    public void onViewClicked() {
        JumpUtil.toLogin(context);
        RxBus.get().send(EventCode.LOGIN_OUT);
    }

    @Override
    public void orderS(List<OrderInfo> orderInfos) {
        if (orderInfos == null){
            refreshLayout.finishLoadmore();
            refreshLayout.finishRefresh();
            setDataView();
            return;
        }
        if (!isLoadMore){
            orders.clear();
            refreshLayout.finishRefresh();
        }else {
            refreshLayout.finishLoadmore();
        }
        if (orderInfos.size() < pageSize){
            hasMoreData = false;
        }
        orders.addAll(orderInfos);
        adapter.notifyDataSetChanged();
        setDataView();
    }

    private void setDataView(){
        if (orders.size() > 0){
            arlContent.setVisibility(View.VISIBLE);
            arlNotData.setVisibility(View.GONE);
        }else {
            arlContent.setVisibility(View.GONE);
            arlNotData.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void orderF(String msg) {
        T.show(msg);
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
        setDataView();
    }
}
